package book;

import java.util.Arrays;

public class BookList {
    private Book[] books = new Book[10];//初始化数组容量为10
    private int usedSize;//书架上的有效书的数量
    //默认每次都是三本书
    public BookList() {
        books[0] = new Book("三国演义","罗贯中","小说",10);
        books[1] = new Book("西游记","吴承恩","小说",15);
        books[2] = new Book("红楼梦","曹雪芹","小说",20);
        usedSize = 3;
    }

    /**
     * 找到 pos 位置的书并返回
     * @param pos
     * @return
     */
    public Book findBook(int pos) {
        return books[pos];
    }

    /**
     * 在 pos 位置新增一本书
     * @param pos
     * @param book
     */
    public void setBook(int pos, Book book) {
        //如果满了，进行扩容
        if(usedSize == books.length) {
            books = Arrays.copyOf(books,2*books.length);
        }
        books[pos] = book;
        usedSize++;
    }

    /**
     * 获取书架上的有效书本的个数并返回
     * @return
     */
    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

}
